﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace WindowsFormsApplication1.WebTest.WebTestXmlNodes
{
    [XmlRoot("RequestPlugin", Namespace = XmlNamespace)]
    public class RequestPluginNode : GenericWebTestNode, IXmlSerializable
    {
        private RuleParametersNode _ruleParameters;

        public override void SetNodeProperties()
        {
            SelectedImageIndex = 7;
            ImageIndex = 7;

            Text = "Request Plugin";
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            Notification.BeginOperation("Processing RequestPlugin", NodeOperationEventArgs.EventImportance.Detailed);

            //Read RequestPlugin attributes
            ReadXmlAttributes(reader);

            //Set node properties such as Icon, Text, etc..
            SetNodeProperties();

            //Read <RequestPlugin> opening tag
            reader.MoveToContent();
            bool isEmptyElement = reader.IsEmptyElement;

            reader.Read();

            var ruleParameters = (RuleParametersNode)_ruleParametersNodeSerializer.Deserialize(reader);

            if (ruleParameters != null)
                foreach (var propertyName in ruleParameters.NodeProperties.Keys)
                    _nodeProperties.Add(propertyName, ruleParameters.NodeProperties[propertyName]);

            _ruleParameters = ruleParameters;

            //Read the ExtractionRule closing tag
            reader.MoveToContent();

            //If there is closing tag read it
            if (!isEmptyElement)
                reader.ReadEndElement();

            Notification.EndOperation();
        }


        #endregion

    }
}
